package com.sztx.pay.center.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.core.service.BankPayLimitService;
import com.sztx.pay.center.core.service.bean.rpc.BankPayLimitInfo;
import com.sztx.pay.center.core.service.bean.rpc.BankPayLimitInfo.LimitInfo;
import com.sztx.pay.center.dataaccess.dao.mysql.BankPayLimitMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.BankPayLimitDO;
import com.sztx.pay.center.dataaccess.domain.BankPayLimitInfoDO;
import com.sztx.se.core.service.impl.BaseServiceImpl;
import com.sztx.se.dataaccess.mysql.config.PageQuery;

@Service
public class BankPayLimitServiceImpl extends BaseServiceImpl implements BankPayLimitService{
	@Autowired
	BankPayLimitMysqlDAO bankPayLimitMysqlDAO;
	
	@Override
	public List<BankPayLimitInfo> getPayLimitList(String bankId,
			String bankName, PageQuery pageQuery) {
		BankPayLimitDO bankPayLimitDO = new BankPayLimitDO();
		bankPayLimitDO.setBankName(bankName);
		bankPayLimitDO.setBankId(bankId);
		List<BankPayLimitInfoDO> payLimitList = bankPayLimitMysqlDAO.getPayLimitList(bankPayLimitDO,pageQuery  );
		List<BankPayLimitInfo> reponse= new ArrayList<BankPayLimitInfo>();
		
		for (BankPayLimitInfoDO bankPayLimitInfoDO : payLimitList) {
			BankPayLimitInfo info = new BankPayLimitInfo();
			List<BankPayLimitDO> bankPayLimitDOS = bankPayLimitInfoDO.getBankPayLimitDO();
			info.setBankId(bankPayLimitInfoDO.getBankId());
			info.setBankName(bankPayLimitInfoDO.getBankName());
			if(bankPayLimitDOS.size() > 0){
				info.setAttach(bankPayLimitDOS.get(0).getAttach());
				info.setCardType(bankPayLimitDOS.get(0).getCardType());
				info.setCreatTime(bankPayLimitDOS.get(0).getCreatTime());
				info.setLastOperator(bankPayLimitDOS.get(0).getLastOperator());
				info.setPayType(bankPayLimitDOS.get(0).getPayType());
				info.setUpdateTime(bankPayLimitDOS.get(0).getUpdateTime());
				List<LimitInfo> limitInfos = new ArrayList<BankPayLimitInfo.LimitInfo>();
				for (BankPayLimitDO bankPayLimitDO2 : bankPayLimitDOS) {
					if(bankPayLimitDO2.getId() != null){
						LimitInfo limitInfo = new BankPayLimitInfo().new LimitInfo();
						limitInfo.setDayLimit(bankPayLimitDO2.getDayLimit());
						limitInfo.setDesc(bankPayLimitDO2.getDescription());
						limitInfo.setMonthLimit(bankPayLimitDO2.getMonthLimit());
						limitInfo.setId(bankPayLimitDO2.getId());
						limitInfo.setSingleLimit(bankPayLimitDO2.getSingleLimit());
						limitInfo.setState(bankPayLimitDO2.getState());
						limitInfo.setLastOperator(bankPayLimitDO2.getLastOperator());
						limitInfos.add(limitInfo);
					}
				}
				info.setLimitInfos(limitInfos);
			}
			reponse.add(info);
		}
		
		return reponse;
	}
	@Override
	public int getPayLimitCount(String bankId, String bankName) {
		BankPayLimitDO bankPayLimitDO = new BankPayLimitDO();
		bankPayLimitDO.setBankName(bankName);
		bankPayLimitDO.setBankId(bankId);
		int count = bankPayLimitMysqlDAO.getPayLimitCount(bankPayLimitDO);
		return count;
	}
	@Override
	public void updateLimitInfo(BankPayLimitInfo limitInfo) {
		ValidateUtil.isNotBlank(limitInfo.getBankId(), "银行ID不能为空");
		Byte cardType = limitInfo.getCardType();
		if(cardType == null){
			cardType = 0;
		}
		BankPayLimitDO bankPayLimitDO = new BankPayLimitDO();
		bankPayLimitDO.setBankId(limitInfo.getBankId());
		bankPayLimitDO.setBankName(limitInfo.getBankName());
		bankPayLimitDO.setPayType(limitInfo.getPayType());
		bankPayLimitDO.setCardType(cardType);
//		List<BankPayLimitInfoDO> payLimitList = bankPayLimitMysqlDAO.getPayLimitList(bankPayLimitDO,null);
//		if(payLimitList.size() != 1){
//			throw new BusinessException("查询银行卡信息错误");
//		}
//		BankPayLimitInfoDO bankPayLimitInfoDO = payLimitList.get(0);
//		List<BankPayLimitDO> bankPayLimitDOS = bankPayLimitInfoDO.getBankPayLimitDO();
		List<LimitInfo> limitInfos = limitInfo.getLimitInfos();
		for (LimitInfo limitInfo2 : limitInfos) {
			bankPayLimitDO.setSingleLimit(limitInfo2.getSingleLimit());
			bankPayLimitDO.setDayLimit(limitInfo2.getDayLimit());
			bankPayLimitDO.setMonthLimit(limitInfo2.getMonthLimit());
			bankPayLimitDO.setDescription(limitInfo2.getDesc());
			bankPayLimitDO.setAttach(limitInfo.getAttach());
			bankPayLimitDO.setLastOperator(limitInfo.getLastOperator());
			bankPayLimitDO.setState(limitInfo2.getState());
			if(limitInfo2.getId() != null && limitInfo2.getId() > 0){
				bankPayLimitDO.setId(limitInfo2.getId());
				bankPayLimitMysqlDAO.update(bankPayLimitDO);
			}else{
				bankPayLimitMysqlDAO.save(bankPayLimitDO);
			}
			
		}
	}
	@Override
	public BankPayLimitInfo getPayLimitInfo(String bankId, int payType) {
		BankPayLimitInfoDO payLimitInfo = bankPayLimitMysqlDAO.getPayLimitInfo(bankId, payType);
		if(payLimitInfo == null){
			return null;
		}
		
		BankPayLimitInfo reponse= new BankPayLimitInfo();
		
		List<BankPayLimitDO> bankPayLimitDOS = payLimitInfo.getBankPayLimitDO();
		reponse.setBankId(payLimitInfo.getBankId());
		reponse.setBankName(payLimitInfo.getBankName());
		if(bankPayLimitDOS.size() > 0){
			reponse.setAttach(bankPayLimitDOS.get(0).getAttach());
			reponse.setCardType(bankPayLimitDOS.get(0).getCardType());
			reponse.setCreatTime(bankPayLimitDOS.get(0).getCreatTime());
			reponse.setLastOperator(bankPayLimitDOS.get(0).getLastOperator());
			reponse.setPayType(bankPayLimitDOS.get(0).getPayType());
			reponse.setUpdateTime(bankPayLimitDOS.get(0).getUpdateTime());
			List<LimitInfo> limitInfos = new ArrayList<BankPayLimitInfo.LimitInfo>();
			for (BankPayLimitDO bankPayLimitDO2 : bankPayLimitDOS) {
				if(bankPayLimitDO2.getId() != null){
					LimitInfo limitInfo = new BankPayLimitInfo().new LimitInfo();
					limitInfo.setDayLimit(bankPayLimitDO2.getDayLimit());
					limitInfo.setDesc(bankPayLimitDO2.getDescription());
					limitInfo.setMonthLimit(bankPayLimitDO2.getMonthLimit());
					limitInfo.setId(bankPayLimitDO2.getId());
					limitInfo.setSingleLimit(bankPayLimitDO2.getSingleLimit());
					limitInfo.setState(bankPayLimitDO2.getState());
					limitInfo.setLastOperator(bankPayLimitDO2.getLastOperator());
					limitInfos.add(limitInfo);
				}
			}
			reponse.setLimitInfos(limitInfos);
		}
		return reponse;
	}

	
	
}
